1 00:00:00,790 --> 00:00:01,780 Welcome back. 2 00:00:01,780 --> 00:00:06,130 We're going to continue where we left off by starting to fill out our game service. 3 00:00:06,130 --> 00:00:11,920 This is the service that will be responsible for listening when our start game event gets fired by our 4 00:00:11,920 --> 00:00:12,250 player. 5 00:00:12,250 --> 00:00:13,360 Join service. 6 00:00:13,360 --> 00:00:20,020 So as you remember in our last lecture, when enough players join the game and then all of the players 7 00:00:20,020 --> 00:00:26,770 have properly loaded, we go ahead and start the game by firing our start game event, and our game 8 00:00:26,770 --> 00:00:31,660 service is going to be responsible for starting up our game so we can go ahead and open this. 9 00:00:31,660 --> 00:00:36,760 And the five services we're going to need in here is first server storage. 10 00:00:39,670 --> 00:00:42,460 We're going to need server script service. 11 00:00:47,830 --> 00:00:50,080 We're going to need replicated storage. 12 00:00:53,310 --> 00:00:55,860 We'll need the players service. 13 00:00:56,760 --> 00:00:59,670 And then we're going to get the team service. 14 00:01:02,000 --> 00:01:03,290 And then we can create a table. 15 00:01:03,290 --> 00:01:08,240 We'll just call it game service, and we'll make sure to return it at the end of our module script here. 16 00:01:09,370 --> 00:01:15,190 Okay, so the few variables we'll need for now in the service is, well, first we want to require a 17 00:01:15,190 --> 00:01:18,610 module script inside of service script service. 18 00:01:18,610 --> 00:01:20,500 So in here we have a module section. 19 00:01:20,500 --> 00:01:23,320 And there is a module script called game sections. 20 00:01:23,320 --> 00:01:28,150 And this module script is going to hold all of the different functions to execute different sections 21 00:01:28,150 --> 00:01:28,840 of the game. 22 00:01:28,840 --> 00:01:32,950 So for example we'll have a beginning section where all the players must do tasks. 23 00:01:32,950 --> 00:01:38,980 And then we'll have a second section where players must survive against our evil AI Squidward monster 24 00:01:38,980 --> 00:01:39,790 in the basement. 25 00:01:39,790 --> 00:01:44,530 So we'll have a section one and a section two, so we can go ahead and require that. 26 00:01:44,530 --> 00:01:49,330 We'll just call this sections, and it's inside of Server Script Service. 27 00:01:49,600 --> 00:01:55,870 In the server folder, get the modules folder and we'll require the games section module script. 28 00:01:55,870 --> 00:01:58,750 And for now in here we will just create a variable. 29 00:01:58,750 --> 00:02:00,880 We'll just call it game sections. 30 00:02:02,050 --> 00:02:03,400 And we'll return it at the end. 31 00:02:03,430 --> 00:02:04,090 For now. 32 00:02:07,410 --> 00:02:13,020 Now back in our game service, what we're going to do next is we're going to require our GUI actions 33 00:02:13,020 --> 00:02:13,710 enum. 34 00:02:15,760 --> 00:02:20,230 Which is inside of replicated storage inside of the modules folder. 35 00:02:20,260 --> 00:02:20,710 Enums. 36 00:02:20,710 --> 00:02:21,880 Dot GUI actions. 37 00:02:21,880 --> 00:02:22,660 Enums. 38 00:02:23,470 --> 00:02:26,380 We want to have a reference to our Start game event. 39 00:02:27,180 --> 00:02:34,110 And that's going to be inside of server storage, dot events, dot bind labels and get our start game 40 00:02:34,110 --> 00:02:35,310 bind event. 41 00:02:35,580 --> 00:02:40,590 And then we're also going to want to have access to an event called Game Communication inside of replicated 42 00:02:40,590 --> 00:02:41,160 storage. 43 00:02:41,160 --> 00:02:46,410 And this game communication remote event is going to be responsible for allowing the client to communicate 44 00:02:46,410 --> 00:02:49,620 to the server and allowing the server to communicate to the client. 45 00:02:49,620 --> 00:02:54,120 So we'll call this game comms event equal to replicated storage. 46 00:02:54,120 --> 00:02:58,680 Dot events dot remotes, dot game communication. 47 00:02:59,570 --> 00:03:06,500 Now we're going to have to create a standard of communication between the client and the server. 48 00:03:06,500 --> 00:03:10,280 And to do that we can create another custom enumerated type. 49 00:03:10,280 --> 00:03:16,640 So inside of replicated storage, inside of modules and an enum, we have a game communication enum 50 00:03:16,640 --> 00:03:21,530 that will allow us to define the different actions that the server can send to clients, and the different 51 00:03:21,530 --> 00:03:23,660 actions that clients can send to the server. 52 00:03:23,810 --> 00:03:25,820 So we'll make a reference to it here. 53 00:03:25,820 --> 00:03:33,500 We'll call it Game Comms enum and we'll require replicated storage, dot modules, dot enums, dot game 54 00:03:33,500 --> 00:03:34,760 communication enum. 55 00:03:34,760 --> 00:03:37,370 And then we can go ahead and open up this enum. 56 00:03:37,370 --> 00:03:40,700 And we can basically just copy what's in here and paste it in here. 57 00:03:40,700 --> 00:03:44,990 But instead what we want to do is we want to define two key value pairs. 58 00:03:44,990 --> 00:03:47,150 One that defines actions to the server. 59 00:03:47,150 --> 00:03:49,760 So we can call it to server. 60 00:03:52,140 --> 00:03:55,500 And one that defines actions that can be given to the client. 61 00:03:55,500 --> 00:03:57,300 So we can call it two client. 62 00:03:58,510 --> 00:04:04,450 So for example, some actions that can be given to the client would be like playing a sound. 63 00:04:04,600 --> 00:04:06,400 So we can call it play sound. 64 00:04:07,650 --> 00:04:11,880 Another action could be like stopping a particular sound from playing. 65 00:04:13,800 --> 00:04:21,810 Another action we could have is to tell the player to change, uh, the audio effects that they have 66 00:04:21,810 --> 00:04:22,710 on their end. 67 00:04:22,710 --> 00:04:30,210 Because inside of our sound service, we have three different type of sound groups. 68 00:04:30,210 --> 00:04:36,300 One that define the different effects that need to be applied for outdoor sounds, indoor sounds and 69 00:04:36,300 --> 00:04:37,530 sounds in the basement. 70 00:04:37,530 --> 00:04:42,780 So when the player enters the building, we want to muffle any outdoor sounds. 71 00:04:42,780 --> 00:04:50,940 And we don't want to muffle any indoor sounds, so we can create an action like change sound to inside 72 00:04:50,940 --> 00:04:57,030 to let the player know to un, muffle any sounds inside, and muffle any sounds that are outside. 73 00:04:57,030 --> 00:04:59,760 And then we can do the same thing for when they enter the basement. 74 00:04:59,760 --> 00:05:01,800 So change sound to basement. 75 00:05:01,800 --> 00:05:06,810 They'll want to keep any sounds that are outside muffled, as well as any sounds that are inside of 76 00:05:06,810 --> 00:05:10,080 the restaurant, muffled because they are inside of the basement. 77 00:05:10,350 --> 00:05:15,360 Another action we could give to the players is to let them know when to start specific scenes. 78 00:05:15,360 --> 00:05:20,220 So we're going to have different camera scenes happen based on the different sections in our game. 79 00:05:20,220 --> 00:05:21,960 So we have our bus here. 80 00:05:21,960 --> 00:05:26,730 If you remember in the beginning where we're going to have the player's camera attached to this bus, 81 00:05:26,730 --> 00:05:31,650 and this bus is going to drive down this road for about 11 seconds until it reaches its destination 82 00:05:31,650 --> 00:05:33,150 and drops the players off. 83 00:05:33,150 --> 00:05:37,800 So we need to have control over when we want to tell the players to start these different scenes. 84 00:05:37,800 --> 00:05:41,340 So one of the actions could be starting the intro scene. 85 00:05:45,170 --> 00:05:49,670 And we could also have an action for when the players begin to enter the basement. 86 00:05:49,670 --> 00:05:54,980 And we're going to be doing that with, uh, the stairs that the players are going to be walking down 87 00:05:54,980 --> 00:05:55,640 into the basement. 88 00:05:55,640 --> 00:05:58,880 So we can call this start stair scene. 89 00:06:04,350 --> 00:06:08,580 Another action we could give is to tell the players when to apply lightning effects. 90 00:06:08,580 --> 00:06:13,530 So the server is going to be the one that's going to be calculating the lightning, and will let the 91 00:06:13,530 --> 00:06:19,200 players know when to do, like lightning sounds and stuff like that on their end. 92 00:06:19,800 --> 00:06:25,380 And then last but not least, is that we're going to have a system where when the player dies, when 93 00:06:25,380 --> 00:06:30,990 they're in the basement after getting hunted by, uh, our Squidward AI, we want to give them the ability 94 00:06:30,990 --> 00:06:33,270 to respawn back into the game. 95 00:06:33,300 --> 00:06:38,610 So there will be a button that they can press to request to respawn their character. 96 00:06:38,610 --> 00:06:44,460 So in our two server section, we can have an action of like request respawn. 97 00:06:48,180 --> 00:06:51,450 And this is the request players will make when they want to respond. 98 00:06:51,450 --> 00:06:56,490 So when they request this, the server will prompt to them to purchase a developer product. 99 00:06:56,490 --> 00:07:02,070 And when that player purchases the developer product and the server verifies it, then we can tell that 100 00:07:02,070 --> 00:07:06,690 particular player to, you know, tell them that, hey, the respawn was granted. 101 00:07:06,690 --> 00:07:12,750 You can, you know, close out your spectating guy and stuff like that so we can give an action to the 102 00:07:12,750 --> 00:07:14,460 client of like respawn. 103 00:07:14,460 --> 00:07:15,450 Granted. 104 00:07:18,290 --> 00:07:22,820 And that's all the actions that we're going to need inside of our game communication enum for now. 105 00:07:23,380 --> 00:07:29,110 So back in our game service, the next thing that we can make a reference to is our update guy event 106 00:07:29,110 --> 00:07:30,580 and replicated storage. 107 00:07:32,490 --> 00:07:35,310 Dot events, dot remotes, dot update guy. 108 00:07:35,490 --> 00:07:41,610 And then we also want to have a reference to the players loaded event and replicated storage as well. 109 00:07:42,600 --> 00:07:47,880 Because when that other service tells us to start the game, then we want to fire to all of the players 110 00:07:47,880 --> 00:07:51,600 that, hey, we're starting the game and you can go ahead and fade out your loading screens. 111 00:07:51,600 --> 00:07:56,640 And then we're also going to make a reference to a part in the workspace called escape Part. 112 00:07:56,640 --> 00:07:59,820 So this is the part that's going to be at the very end of our game. 113 00:07:59,820 --> 00:08:04,920 After the players have successfully regained power back in the basement, they need to escape to a door 114 00:08:04,920 --> 00:08:07,290 that will open and that door has a part in it. 115 00:08:07,290 --> 00:08:12,450 The players need to touch in order to escape and then once they've escaped, then we can display on 116 00:08:12,450 --> 00:08:17,250 their screen stuff like how much money they earned, how many tasks they completed, and then we can 117 00:08:17,250 --> 00:08:18,810 teleport them back to the lobby. 118 00:08:18,810 --> 00:08:24,660 So we'll make a reference to this escape part, and it's inside of the workspace underneath the Krusty 119 00:08:24,660 --> 00:08:25,830 Krab folder. 120 00:08:25,830 --> 00:08:27,870 And there's a folder in there called basement. 121 00:08:28,260 --> 00:08:31,350 And inside of this folder is our escape part. 122 00:08:32,110 --> 00:08:32,560 Okay. 123 00:08:32,560 --> 00:08:34,900 Now we're going to need some private functions. 124 00:08:34,900 --> 00:08:37,960 One function is going to be for starting our game. 125 00:08:38,720 --> 00:08:44,210 We're going to need a function to listen for when a character is added to a player. 126 00:08:44,210 --> 00:08:48,470 And this is because we want to listen for when that character dies and when that character dies, we 127 00:08:48,470 --> 00:08:50,630 want to set their team to the dead team. 128 00:08:51,260 --> 00:08:56,690 We're also going to want to listen for when a player is added to a team, or more specifically, we 129 00:08:56,690 --> 00:08:59,900 want to listen to when a player is added to the dead team. 130 00:08:59,900 --> 00:09:04,490 Because when this happens, what we want to do is we want to remove any tools that may be inside of 131 00:09:04,490 --> 00:09:07,160 the player's backpack or on their character. 132 00:09:07,670 --> 00:09:13,100 And then this service is only going to need an initialize function, because all the service is going 133 00:09:13,100 --> 00:09:14,870 to do is listen to events. 134 00:09:14,870 --> 00:09:17,690 It doesn't have to start or run any code. 135 00:09:18,140 --> 00:09:21,320 One of those events we need to listen to is our start game event. 136 00:09:21,320 --> 00:09:27,590 So when that gets fired, we of course need to start our game so we can call our start game function. 137 00:09:28,580 --> 00:09:35,570 And we could actually instead connect the start game function itself as a reference to this event. 138 00:09:35,570 --> 00:09:41,180 And what we could do inside of here is we could send out a warning like the game is starting. 139 00:09:42,680 --> 00:09:47,450 And then what we want to do is we want to access our players loaded event and fire to all the clients 140 00:09:47,450 --> 00:09:49,730 in our game that everybody has successfully loaded. 141 00:09:49,730 --> 00:09:55,760 That way they can fade out their loading screens, and then we're going to use our Gamescom event and 142 00:09:55,760 --> 00:10:00,620 fire to all of the clients using the action of game comms. 143 00:10:00,620 --> 00:10:01,370 Enum. 144 00:10:02,620 --> 00:10:07,420 Dot two client dot start the intro scene. 145 00:10:07,960 --> 00:10:13,360 And since this scene is going to require them to attach their cameras to the bus, we can pass a table 146 00:10:13,360 --> 00:10:15,880 with a reference to that bus inside of it. 147 00:10:15,880 --> 00:10:16,570 So we can call. 148 00:10:16,570 --> 00:10:21,970 This bus is equal to workspace Dot outdoor Dot bus. 149 00:10:22,390 --> 00:10:27,790 And since we're going to be tweening this bus for about 11 seconds on the client, and we can't keep 150 00:10:27,820 --> 00:10:31,630 track of those tweens on the server, we're just going to put a wait statement in here that's going 151 00:10:31,630 --> 00:10:33,730 to wait for 11 seconds. 152 00:10:33,910 --> 00:10:38,920 And then after those 11 seconds are up, we're going to use our update UI event and fire to all the 153 00:10:38,920 --> 00:10:46,810 clients that we need to have them update the objectives frame on their screen, because now we need 154 00:10:46,810 --> 00:10:48,580 to add a bunch of objectives for them. 155 00:10:48,580 --> 00:10:50,110 So we'll pass a table. 156 00:10:50,110 --> 00:10:54,520 And inside of this table we're going to have other tables that represent the different objectives they 157 00:10:54,520 --> 00:10:57,550 need to add to their objective guy. 158 00:10:57,550 --> 00:11:03,220 So the name of one of the objectives would be money or AKA this is how much money they need to earn 159 00:11:03,220 --> 00:11:04,900 to pay rent. 160 00:11:05,350 --> 00:11:13,030 And we can pass alongside of this name a key value pair, uh, some text to define what exactly they 161 00:11:13,030 --> 00:11:14,590 need to do for this objective. 162 00:11:14,590 --> 00:11:16,690 And for now, we're just going to leave it blank. 163 00:11:16,930 --> 00:11:21,460 And then we'll have another objective of entering into the building. 164 00:11:21,460 --> 00:11:25,870 So we need to tell the players that, hey, you need to enter into the Krusty Krab. 165 00:11:25,870 --> 00:11:28,960 And we can provide text with this objective as well. 166 00:11:28,960 --> 00:11:33,400 But we're going to leave that blank for now, because what we're going to do is we're going to go back 167 00:11:33,400 --> 00:11:35,980 into our game sections module script. 168 00:11:36,220 --> 00:11:42,190 And in here is where we're going to define the text for some of the objectives. 169 00:11:42,190 --> 00:11:49,720 So inside of this public variable section I'm going to add to our game sections uh different objectives 170 00:11:49,720 --> 00:11:51,160 that can be fulfilled in our game. 171 00:11:51,160 --> 00:11:53,440 So we'll have a table called objectives. 172 00:11:53,440 --> 00:11:57,790 And in here we can have for example an objective of money. 173 00:11:57,970 --> 00:12:10,150 And we want to have the text for this be um something like earn $500 to pay for rent. 174 00:12:10,480 --> 00:12:15,610 And then another objective we could have would be to enter into the Krusty Krab. 175 00:12:15,760 --> 00:12:23,980 So this would be text is equal to, uh, something like enter the Krusty Krab. 176 00:12:23,980 --> 00:12:29,590 And then we can also have a Boolean in here to keep track of whether or not this objective was fulfilled 177 00:12:29,590 --> 00:12:30,010 or not. 178 00:12:30,010 --> 00:12:31,810 And we're going to set it to false for now. 179 00:12:32,050 --> 00:12:35,980 So that means back in our game service we can replace this text here. 180 00:12:36,470 --> 00:12:41,390 With the text that we supplied inside of the game sections. 181 00:12:41,390 --> 00:12:44,900 So sections dot objectives. 182 00:12:45,680 --> 00:12:49,700 Dot money dot text for this text. 183 00:12:49,700 --> 00:12:53,150 And then we want to do the same thing here for the enter objective. 184 00:12:53,150 --> 00:12:57,200 So sections dot objectives dot enter. 185 00:12:57,200 --> 00:13:00,410 And we can set this equal to that text. 186 00:13:00,410 --> 00:13:04,520 Now there's actually a couple of other parts we want to make references to. 187 00:13:04,520 --> 00:13:09,500 One is going to be a part inside of the building that the players will touch. 188 00:13:09,770 --> 00:13:15,590 Uh to start the first or basically the other scene of where they need to talk to Squidward. 189 00:13:15,590 --> 00:13:24,830 So inside of the workspace there is a part and we can just call it something like Inside Part, and 190 00:13:24,830 --> 00:13:30,380 that's in the workspace inside of the filtering folder, because that's where all of the invisible parts 191 00:13:30,380 --> 00:13:31,220 are stored. 192 00:13:31,220 --> 00:13:37,250 And inside of there there's a folder called touch parts and the part is inside. 193 00:13:37,370 --> 00:13:40,520 And then we'll also have a touch part inside of the basement. 194 00:13:40,520 --> 00:13:42,530 So we can call this basement part. 195 00:13:42,530 --> 00:13:47,450 And that's equal to workspace dot filtering folder dot touch parts dot basement. 196 00:13:47,450 --> 00:13:53,300 Because now what we can do is inside of our initialize section we can refer to something like our indoor 197 00:13:53,300 --> 00:13:57,710 part or inside part and listen to when it is touched. 198 00:13:57,710 --> 00:14:02,030 And when that gets touched, then we know to start a particular section. 199 00:14:02,030 --> 00:14:04,760 And then the same thing for the basement part. 200 00:14:04,760 --> 00:14:08,180 When that gets touched, then we know to start the basement section. 201 00:14:08,960 --> 00:14:12,800 And last but not least, we can listen for when the escape part gets touched. 202 00:14:12,800 --> 00:14:17,570 And that's where we will, you know, wrap up the game for this particular player. 203 00:14:17,750 --> 00:14:21,410 Now we're also going to want to listen to when a player gets added into the game. 204 00:14:21,410 --> 00:14:25,730 So that player added, we'll just connect a function to this and get the player. 205 00:14:25,730 --> 00:14:30,950 And what we want to do is we want to listen for when that player has a character added to them, and 206 00:14:30,950 --> 00:14:34,820 we'll connect our on character added function to this event. 207 00:14:35,060 --> 00:14:37,520 So now we can go ahead and fill out this function. 208 00:14:37,760 --> 00:14:41,870 And what we want to do in here is I'll actually create another function inside of this function. 209 00:14:41,870 --> 00:14:46,190 And we'll just call it on humanoid humanoid death. 210 00:14:46,190 --> 00:14:52,040 And what this function will do is it will get the player from this character using the player service, 211 00:14:52,040 --> 00:14:54,980 get player from character and we'll pass the character object. 212 00:14:55,510 --> 00:14:59,380 And we want to set the team of this player equal to the dead team. 213 00:14:59,380 --> 00:15:01,240 So teams dot dead. 214 00:15:01,880 --> 00:15:06,500 And then what we could do is we could just simply get the humanoid that is inside of our character, 215 00:15:06,500 --> 00:15:13,790 listen to when it gets killed, and then we'll connect our on humanoid death function to this event. 216 00:15:13,820 --> 00:15:18,500 Now, the other event we want to listen to inside of our initialize section is when a player gets added 217 00:15:18,500 --> 00:15:19,370 to our dead team. 218 00:15:19,370 --> 00:15:22,700 So teams dot dead dot player added. 219 00:15:22,700 --> 00:15:26,030 We'll connect our on player added to team function. 220 00:15:26,030 --> 00:15:32,330 And inside of here, what we want to do is we want to get the player's backpack and we want to call 221 00:15:32,330 --> 00:15:38,120 the clear all children function on it so it destroys any tools that the player might have. 222 00:15:38,330 --> 00:15:43,820 And then we want to check if this player has a character, because if they do, they might have a tool 223 00:15:43,820 --> 00:15:44,360 equipped. 224 00:15:44,360 --> 00:15:49,490 So we want to get this tool by accessing our player's character. 225 00:15:49,490 --> 00:15:54,590 And we want to find first child which is a tool. 226 00:15:55,040 --> 00:15:57,350 And if there is a tool then we need to destroy it. 227 00:15:57,350 --> 00:16:00,830 So if tool then tool destroy. 228 00:16:01,450 --> 00:16:07,240 So now we should be able to test this out by seeing if our loading screen fades out when we have loaded 229 00:16:07,240 --> 00:16:08,050 into the game. 230 00:16:08,620 --> 00:16:15,700 So if I hit play here, we're going to see our loading screen and it perfectly fades out because our 231 00:16:15,700 --> 00:16:19,420 game has started and now we're stuck in spawn for now. 232 00:16:19,420 --> 00:16:25,270 But at least so far, we can tell that everything is working as it's supposed to and we have no errors 233 00:16:25,270 --> 00:16:26,140 in the console. 234 00:16:26,410 --> 00:16:29,620 Thanks for sticking around for this lecture and I'll see you in the next one.